import {
  companyTypeList,
  companyTypeInsert,
  companyTypeDel,
  companyTypeEdit,
  compTypeResTree,
  compTypeResSave
} from '../services/company'
import { message } from "antd";

export default {
  namespace: 'companyType',

  state: {
    addForm: {},
    searchForm: {},
    modalForm: {},
    list: [],
    selection: {},
    modalVisible: false,
    optFlag: '',
    compTypeOptions: [],
    treeModalTitle: '',
    treeModalVisible: false,
    treeData: [],
    treeSelectKeys: [],
    activeTypeId: '',
    treeSubmitData: [],
    expandedKeys: [],
    autoExpandParent: true
  },

  effects: {
    * page({payload}, {call, put}) {
      const response = yield call(companyTypeList, payload);
      if (response && response.code === 0) {
        const list = response.result;
        yield put({
          type: 'queryList',
          payload: list,
        });
      }

    },

    * insert({payload, callback}, {call, put}) {
      const {fields, searchForm} = payload;
      const response = yield call(companyTypeInsert, fields);
      if (response && response.code === 0) {
        message.success('添加成功');
        yield put({
          type: "page",
          payload: {...searchForm}
        });
        yield put({
          type: 'handleModalVisible',
          payload: {modalVisible: false}
        });
        yield put({
          type: "handleCleanAddForm",
          payload: {}
        });
        yield put({
          type: 'getOptions'
        });
      }
    },

    * del({payload, callback}, {call, put}) {
      const {fields, searchForm} = payload;
      const response = yield call(companyTypeDel, fields);
      if(response && response.code === 0){
        message.success('删除成功');
        yield put({
          type: 'page',
          payload: {...searchForm}
        });
        yield put({
          type: "handleTableSelection",
          payload: {}
        });
        yield put({
          type: 'handleModalVisible',
          payload: {modalVisible: false}
        });
        yield put({
          type: 'getOptions'
        });
      }
    },

    * update({payload, callback}, {call, put}) {
      const {fields, searchForm} = payload;
      const response = yield call(companyTypeEdit, fields);
      if(response && response.code === 0){
        message.success('修改成功');
        yield put({
          type: "page",
          payload: {...searchForm}
        });
        yield put({
          type: "handleTableSelection",
          payload: fields
        });
        yield put({
          type: 'handleModalVisible',
          payload: {modalVisible: false}
        });
        yield put({
          type: 'getOptions'
        });
      }
    },

    * getOptions({},{call, put}){
      const response = yield call(companyTypeList);
      if (response && response.code === 0) {
        const list = response.result;
        yield put({
          type: 'putOptions',
          payload: list,
        });
      }
    },

    * getTreeData({payload}, {call, put}) {
      const res = yield call(compTypeResTree, payload);
      if (res && res.code === 0) {
        yield put({
          type: 'putTreeData',
          payload: res.result
        });
      }
    },

    * compTypeResSave({payload}, {call, put}) {
      const res = yield call(compTypeResSave, payload);
      if (res && res.code === 0) {
        message.success('保存成功');
        yield put({
          type: 'handleTreeVisible',
          payload: false
        });
        yield put({
          type: 'handleTreeSubmitData',
          payload: []
        });
      }
    }
  },

  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    handleSearchChange(state, action) {
      const {searchForm} = state;
      const newSearchForm = {
        ...searchForm,
        ...action.payload
      }
      return {
        ...state,
        searchForm: newSearchForm
      }
    },
    handleModalVisible(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },

    handleTableSelection(state, action) {
      return {
        ...state,
        selection: action.payload
      }
    },

    handleModalChange(state, action) {
      const {modalForm} = state;
      const {fields, optFlag} = action.payload;
      const newModalForm = {
        ...modalForm,
        ...fields
      }
      let st = {
        ...state,
        modalForm: newModalForm
      }
      if(optFlag === "add"){
        let { addForm }= action.payload
        Object.keys(fields).map(key => {
          addForm[key] = fields[key]['value'];
        })
        st = {
          ...st,
          addForm: addForm
        }
      }
      return st
    },
    handleCleanAddForm(state, action) {
      return {
        ...state,
        addForm: action.payload
      }
    },
    handleTreeVisible(state, action) {
      return{
        ...state,
        treeModalVisible: action.payload
      }
    },
    putTreeData(state, action) {
      return{
        ...state,
        treeSelectKeys: action.payload.checkedKeys,
        treeData: action.payload.treeData,
        expandedKeys: action.payload.expandedKeys,
        autoExpandParent: true
      }
    },
    handleTreeTitleChange(state, action) {
      return{
        ...state,
        treeModalTitle: action.payload
      }
    },
    handleActiveTypeChange(state, action) {
      return{
        ...state,
        activeTypeId: action.payload
      }
    },
    handleSelectKeys(state, action) {
      return{
        ...state,
        treeSelectKeys: action.payload
      }
    },
    handleTreeSubmitData(state, action) {
      return{
        ...state,
        treeSubmitData: action.payload
      }
    },
    putOptions(state, action) {
      const types = action.payload;
      let options = [];
      types.map(item => {
        let option = {};
        option.value = item.code;
        option.label = item.name;
        options.push(option);
      })
      return {
        ...state,
        compTypeOptions: options
      }
    },
    treeExpand(state, action) {
      return{
        ...state,
        ...action.payload
      }
    }
  },


}
